//
//  UIColor+Extension.swift
//  SLQuickReader
//
//  Created by ALin on 2017/8/3.
//  Copyright © 2017年 ALin. All rights reserved.
//

import UIKit

extension UIColor
{
    public class func color(r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat = 1.0) -> UIColor{
        return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
    }
    
    public class func grayColor(gray: CGFloat) -> UIColor{
        return color(r: gray, g: gray, b: gray, a: 1.0)
    }
    
    public class func hexColor(_ value: Int) -> UIColor{
        return UIColor.color(r: ((CGFloat)((value & 0xFF0000) >> 16)), g: ((CGFloat)((value & 0xFF00) >> 8)), b: ((CGFloat)(value & 0xFF)))
    }
    
    static func colorWithHexString (hex: String) -> UIColor {
        
        var cString: String = hex.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines).uppercased()
        
        if (cString.hasPrefix("#")) {
            cString = (cString as NSString).substring(from: 1)
        }
        
        if (cString.characters.count != 6) {
            return UIColor.clear
        }
        
        let rString = (cString as NSString).substring(to: 2)
        let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)
        let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)
        
        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
        Scanner(string: rString).scanHexInt32(&r)
        Scanner(string: gString).scanHexInt32(&g)
        Scanner(string: bString).scanHexInt32(&b)
        
        return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
    }
    
}
